package com.zidian.study.other;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.zidian.study.utils.DensityUtils;

import com.zidian.student.trucks.R;

/**
 * @{# OnSlidePageChangeListener.java Create on 2015-10-21 下午7:15:45
 * <p>
 * class desc: 幻灯片轮播页数改变监听器 <li>
 * 要实现自动播放，需要在对应Activity或Fragment中调用onActivityStart和onActivityStop方法
 * Copyright: Copyright(c) 2015
 * </p>
 * @Version 1.0   Panca  QQ643691522
 */
public class OnSlidePageChangeListener implements OnPageChangeListener {
    private ViewPager viewPager;
    private List<ImageView> indicatorImages;
    private ScheduledExecutorService executorService;
    private static final int WHAT_CHANGE_PAGE = 1;

    public OnSlidePageChangeListener(ViewPager viewPager,
                                     LinearLayout indicatorContainer, int pageSize) {
        super();
        this.viewPager = viewPager;
        indicatorImages = new ArrayList<ImageView>();
        for (int i = 0; i < pageSize; i++) {
            ImageView dotView = new ImageView(viewPager.getContext());
            LayoutParams params = new LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, 1);
            params.leftMargin = DensityUtils.dip2px(viewPager.getContext(), 3);
            indicatorContainer.addView(dotView, i, params);
            indicatorImages.add(i, dotView);
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < indicatorImages.size(); i++) {
            if (i == position) {
                indicatorImages.get(i).setImageResource(
                        R.drawable.ic_indicator_active);
            } else {
                indicatorImages.get(i).setImageResource(
                        R.drawable.ic_indicator_normal);
            }
        }
    }

    /**
     * handler
     */
    private Handler handler = new Handler(new Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case WHAT_CHANGE_PAGE:
                    if (viewPager != null && viewPager.getAdapter() != null) {
                        int curr = viewPager.getCurrentItem();
                        if (++curr < viewPager.getAdapter().getCount()) {
                            viewPager.setCurrentItem(curr, true);
                        } else {
                            viewPager.setCurrentItem(0, true);
                        }
                    }
                    break;

                default:
                    break;
            }
            return false;
        }
    });

    /**
     * Activity onStart
     */
    public void onActivityStart() {
        executorService = Executors.newSingleThreadScheduledExecutor();
        // 当Activity显示出来后，每两秒钟切换一次图片显示
        executorService.scheduleAtFixedRate(new Runnable() {

            public void run() {
                handler.obtainMessage(WHAT_CHANGE_PAGE).sendToTarget();
            }
        }, 1, 3, TimeUnit.SECONDS);
    }

    /**
     * Activity onStop
     */
    public void onActivityStop() {
        if (executorService != null) {
            executorService.shutdown();
        }
    }

}
